/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ArugulaCodePanel.java
 *
 * Created on 22-mar-2010, 11.18.24
 */

package ch.epfl.dslab.conferrng.wizard;

import ch.epfl.dslab.conferrng.arugula.ErrorGenerator;
import ch.epfl.dslab.conferrng.arugula.Operator;
import ch.epfl.dslab.conferrng.arugula.OperatorChangedListener;
import ch.epfl.dslab.conferrng.engine.FaultInjectionPlan;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.StringWriter;

import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author lokeller
 */
public class ArugulaCodePanel extends javax.swing.JPanel implements PropertyChangeListener, OperatorChangedListener {

    /** Creates new form ArugulaCodePanel */
    public ArugulaCodePanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setLayout(new java.awt.BorderLayout());

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents


    private FaultInjectionPlan plan;

    public FaultInjectionPlan getPlan() {
        return plan;
    }

    public void setPlan(FaultInjectionPlan plan) {
        if (this.plan != null) {
            plan.removePropertyChangeListener(FaultInjectionPlan.PROP_ERRORGENERATORS, this);
            for ( ErrorGenerator eg : plan.getErrorGenerators()) {
                eg.removeOperatorChangeListener(this);
            }
        }

        this.plan = plan;
        
        if (this.plan != null) {
            plan.addPropertyChangeListener(FaultInjectionPlan.PROP_ERRORGENERATORS, this);
            for ( ErrorGenerator eg : plan.getErrorGenerators()) {
                eg.addOperatorChangeListener(this);
            }
        }
    }

    private void updateCode() {
        try {

            StringWriter writer = new StringWriter();
            XMLOutputter outputter = new XMLOutputter();
            outputter.setFormat(Format.getPrettyFormat());
            outputter.output(plan.saveErrorGeneratorsToFile(), writer);
            jTextArea1.setText(writer.toString());
        } catch (IOException ex) {
            Logger.getLogger(ArugulaCodePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        for (ErrorGenerator eg : (Vector<ErrorGenerator>) evt.getOldValue() ) {
            eg.removeOperatorChangeListener(this);
        }

        for (ErrorGenerator eg : (Vector<ErrorGenerator>) evt.getNewValue() ) {
            eg.addOperatorChangeListener(this);
        }

        updateCode();

    }

    @Override
    public void operatorChanged(Operator o) {
        updateCode();
    }

}
